package uit.nfcmenu.models;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;

public class ItemAdapter extends DatabaseAdapter {

	public ItemAdapter(Context context) {
		super(context);
	}

	public int insertItemModel(ItemModel model) {
		int value = -1;

		this.open();

		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();

			contentValues.put(DbDefines.TableItem.itemID, model.getItemID());
			contentValues
					.put(DbDefines.TableItem.itemName, model.getItemName());
			contentValues.put(DbDefines.TableItem.menuID, model.getMenuID());
			contentValues.put(DbDefines.TableItem.imageLink,
					model.getImageLink());
			contentValues.put(DbDefines.TableItem.description,
					model.getDescription());
			contentValues
					.put(DbDefines.TableItem.discount, model.getDiscount());

			// Add to database
			value = (int) this.mDb.insert(DbDefines.ItemModel, "",
					contentValues);
		} finally {
			this.close();
		}

		return value;
	}

	public int deleteItemModel(int itemID) {
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.ItemModel,
					DbDefines.TableItem.itemID + " = " + itemID, null);
		} finally {
			this.close();
		}
		return result;
	}

	public int updateItemModel(ItemModel model) {
		int value = -1;
		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();
			contentValues.put(DbDefines.TableItem.itemID, model.getItemID());
			contentValues
					.put(DbDefines.TableItem.itemName, model.getItemName());
			contentValues.put(DbDefines.TableItem.menuID, model.getMenuID());
			contentValues.put(DbDefines.TableItem.imageLink,
					model.getImageLink());
			contentValues.put(DbDefines.TableItem.description,
					model.getDescription());
			contentValues
					.put(DbDefines.TableItem.discount, model.getDiscount());
			// Add to database
			value = (int) this.mDb.update(DbDefines.ItemModel, contentValues,
					DbDefines.TableItem.itemID + " = " + model.getItemID(),
					null);
			if (value == 0) {
				this.close();
				return insertItemModel(model);
			}
		} finally {
			this.close();
		}
		return value;
	}

	public ItemModel loadItemModel(int itemID) {
		this.open();
		// getting data from database
		Cursor cursor = null;
		ItemModel model = null;
		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.itemID, DbDefines.TableItem.itemName,
					DbDefines.TableItem.menuID, DbDefines.TableItem.imageLink,
					DbDefines.TableItem.description,
					DbDefines.TableItem.discount }, DbDefines.TableItem.itemID
					+ " = " + "'" + itemID + "'", null, "", "", "", "1");

			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				model = new ItemModel(cursor);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return model;
	}

	public ArrayList<ItemModel> loadAllItem() {
		Cursor cursor = null;
		ItemModel model = null;
		ArrayList<ItemModel> models = new ArrayList<ItemModel>();
		this.open();
		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.itemID, DbDefines.TableItem.itemName,
					DbDefines.TableItem.menuID, DbDefines.TableItem.imageLink,
					DbDefines.TableItem.description }, "", null, "", "", "",
					"1");

			while (cursor.moveToNext()) {
				model = new ItemModel(cursor);
				models.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return models;
	}

	// =================================
	// MORE
	// =================================

	public void updateAllItems(ArrayList<ItemModel> models) {
		// TODO Auto-generated method stub
		for (ItemModel model : models) {
			updateItemModel(model);
		}
	}

	public ArrayList<ItemModel> getItemsByMenu(int mainId) {
		// TODO Auto-generated method stub
		ArrayList<ItemModel> models = new ArrayList<ItemModel>();
		this.open();
		// getting data from database
		Cursor cursor = null;
		ItemModel model = null;

		try {
			cursor = this.mDb.query(DbDefines.ItemModel, new String[] {
					DbDefines.TableItem.itemID, DbDefines.TableItem.itemName,
					DbDefines.TableItem.menuID, DbDefines.TableItem.imageLink,
					DbDefines.TableItem.description,
					DbDefines.TableItem.discount }, DbDefines.TableItem.menuID
					+ " = " + "'" + mainId + "'", null, "", "", "", "");

			while (cursor.moveToNext()) {
				model = new ItemModel(cursor);
				models.add(model);
			}

		} catch (SQLiteException e) {
			e.printStackTrace();

		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}

		return models;
	}

	public ArrayList<ItemModel> selectAll() {
		// TODO Auto-generated method stub

		this.open();

		// getting data from database
		Cursor cursor = null;
		ItemModel model = null;
		ArrayList<ItemModel> models = new ArrayList<ItemModel>();
		try {
			cursor = mDb.rawQuery("SELECT * FROM " + DbDefines.ItemModel, null);

			while (cursor.moveToNext()) {
				model = new ItemModel(cursor);
				models.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return models;
	}
}
